import 'package:flutter/material.dart';
import 'package:whale/helper/component/component.dart';
import 'package:whale/helper/utils/util.dart';
import 'dialog.dart';
import 'index.dart';

class SelectionController extends GetxController {
  final GlobalKey titleKey = GlobalKey();
  Function(SelectionResultModel res)? complete;
  SelectionModel? mod;
  @override
  void onInit() {
    super.onInit();
  }

  void actionTitle({
    required int index,
    required SelectionModel md,
    Function(SelectionResultModel res)? complete,
  }) {
    this.complete = complete;
    var mq = MediaQuery.of(Get.context!);
    var box = titleKey.currentContext!.findRenderObject() as RenderBox;
    var off = box.localToGlobal(Offset(box.size.width, box.size.height));
    var top = off.dy - mq.padding.bottom;

    showSelectionDialog(top: top, md: md, controller: this);
  }

  void actionChooseDate({
    required SelectionModel md,
    SelectionModel? parent,
  }) {
    showFilterDate(
      complete: (p0) {
        var beginTimeStr =
            DateFormat.format(p0.start, DateFormatType.ymd, dateSeprator: '-');

        var endTimeStr =
            DateFormat.format(p0.end, DateFormatType.ymd, dateSeprator: '-');
        Get.log("$beginTimeStr,$endTimeStr");
        md.text = "发布时间";
        md.val = "$beginTimeStr,$endTimeStr";
        if (parent?.list != null) {
          for (var i = 0; i < parent!.list!.length; i++) {
            var e = parent.list![i];
            if (e.tag == md.relateTag) {
              e.list?.forEach((e2) {
                e2.isSelected = false;
              });
            }
          }
        }

        update();
      },
    );
  }

  void actionItem({
    required SelectionModel md,
    SelectionModel? parent,
  }) {
    if (md.itemStyle == SelectionItemStyle.date) {
      actionChooseDate(md: md, parent: parent);
      return;
    }
    if (parent?.tag != 0 || parent?.relateTag != null) {
      mod?.list?.forEach((e) {
        if (e.relateTag == parent?.tag || parent?.tag == e.tag) {
          e.list?.forEach((e2) {
            e2.isSelected = false;
            e2.val = '';
          });
          e.val = '';
        }
      });
    }

    if (parent == null) {
      return;
    }
    if (md.isShow == false && !isZeroList(md.list)) {
      parent.list?.forEach((element) {
        element.isShow = false;
      });
      md.isShow = true;
      update();
      return;
    }

    if (parent.isMutiable != true) {
      parent.list?.forEach((element) {
        if (element.text != md.text) {
          element.isSelected = false;
        }
      });
    }

    md.isSelected = !md.isSelected;

    if (md.isMutiable == false) {
      parent.list?.forEach((element) {
        if (element.isMutiable == true) {
          if (element.text != md.text) {
            element.isSelected = md.isSelected;
            element.list?.forEach((e2) {
              e2.isSelected = md.isSelected;
            });
          }
        }
      });
    } else {
      var fmd = parent.list!.first;
      if (fmd.isMutiable == false) {
        if (fmd.isSelected) {
          fmd.isSelected = parent.list!
              .where((element) => element.isSelected == false)
              .isEmpty;
        } else {
          fmd.isSelected = parent.list!
                  .where((element) => element.isSelected == false)
                  .length ==
              1;
        }
      }
    }

    if (md.list != null) {
      for (var element in md.list!) {
        element.isSelected = md.isSelected;
      }
    }

    parent.isSelected =
        parent.list?.where((element) => element.isSelected == false).isEmpty ??
            false;

    update();
  }

  void actionReset(SelectionModel md) {
    md.isSelected = false;
    md.list?.forEach((e1) {
      e1.isSelected = false;
      e1.val = '';
      e1.list?.forEach((e2) {
        e2.isSelected = false;
        e2.val = '';
      });
    });
    update();
  }

  void actionSure(SelectionModel md) {
    // SelectionModel newMd = SelectionModel.copy(md);
    // newMd.list = [];
    List<String> val = [];
    List<SelectionResultModel> items = [];
    if (md.list != null) {
      int? relateTag;
      for (var e1 in md.list!) {
        if (e1.itemStyle == SelectionItemStyle.date ||
            e1.itemStyle == SelectionItemStyle.input ||
            e1.itemStyle == SelectionItemStyle.custom) {
          if (e1.val.isNotEmpty) {
            items.add(
              SelectionResultModel(key: e1.title ?? e1.text, val: e1.val),
            );
            if (e1.relateTag != null) {
              relateTag = e1.relateTag;
            }
          }
          continue;
        }
        if (e1.tag == relateTag) {
          continue;
        }
        if (e1.text == '全部' && e1.isSelected) {
          val.add(e1.text);
          break;
        }
        if (e1.list?.first.isMutiable == false &&
            e1.list?.first.isSelected == true) {
          items.add(
            SelectionResultModel(
                key: e1.title ?? e1.text, val: e1.list?.first.text ?? ''),
          );
          continue;
        } else if (e1.isSelected) {
          val.add(e1.text);
        } else {
          var itemVal = e1.list
              ?.where((e2) => e2.isSelected)
              .map((e) => e.text)
              .join(',');

          if (!isNilStr(itemVal)) {
            items.add(
                SelectionResultModel(key: e1.title ?? e1.text, val: itemVal));
          }
        }
      }
    }

    var res = SelectionResultModel(
      key: md.text,
      val: val.join(','),
      items: items,
    );
    if (complete != null) {
      complete!(res);
    }
    Get.back();
  }

  @override
  void onReady() {
    // TODO: implement onReady
    super.onReady();
  }

  @override
  void onClose() {
    // TODO: implement onClose
    super.onClose();
  }
}
